﻿using System;
using System.Windows;
using Caliburn.Core;
using Caliburn.PresentationFramework;
using Caliburn.PresentationFramework.ApplicationModel;
using Caliburn.Prism;
using Caliburn.Unity;
using Merbla.Caliburn.Presenters;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.ServiceLocation;
using Merlba.MVVM.Module.Football;
using Microsoft.Practices.Unity;

namespace Merbla.Caliburn
{
public class BootStrapper
{
    private Application _application;
    private readonly UnityAdapter _unityAdapter;

    public BootStrapper(Application application)
    {
        _application = application;
        _unityAdapter = new UnityAdapter(new UnityContainer());
    }

    public void Run()
    {
        RegisterDependencies();
                  
        CaliburnFramework
           .ConfigureCore(_unityAdapter)
           
           .AfterStart(() =>
           {
               var binder = (DefaultBinder)ServiceLocator.Current.GetInstance<IBinder>();
               binder.EnableMessageConventions();
               binder.EnableBindingConventions();
           })
           .WithPresentationFramework()
           .WithCompositeApplicationLibrary(CreateShell )
           .WithModuleCatalog(new ModuleCatalog().AddModule(typeof(FootballModule)))
           .Start();
    }

    private void RegisterDependencies()
    {
        //Register dependencies with Unity
    }

    private DependencyObject CreateShell()
    {
        ServiceLocator.Current.GetInstance<IWindowManager>()
            .Show(ServiceLocator.Current.GetInstance<ShellPresenter>(), null, ExecuteShutdownModel);
        return _application.MainWindow;
    }

    private void ExecuteShutdownModel(ISubordinate subordinate, Action completed)
    {
        completed();
    }
}
}
